
package com.hlkj.pay.controller.order;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;

import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.common.IDownloadExcelService;
import com.hlkj.pay.app.order.IReceiveOrderAppService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.order.OrderReceiveDto;
import com.hlkj.pay.dto.order.OrderReceiveQueryDto;
import com.hlkj.pay.dto.order.PayOrderQueryDto;
import com.hlkj.pay.infrastructure.model.order.OrderReceiveDO;
import com.hlkj.pay.util.DateUtils;
import com.hlkj.pay.vo.openapi.order.resp.PayOrderSubmitApiResp;
import com.hlkj.pay.vo.order.req.*;
import com.hlkj.pay.vo.order.resp.PayOrderDetailResp;
import com.hlkj.pay.vo.order.resp.export.PayOrderFileExport;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/09 11:25
 */
@Tag(name = "收款单管理")
@RestController
@RequestMapping("/v1/order/receive")
@RequiredArgsConstructor
@Slf4j
public class ReceiveOrderController extends BaseController {

    private final IReceiveOrderAppService receiveOrderAppService;

    private final IDownloadExcelService downloadExcelService;

    @Operation(summary = "收款单列表分页")
    @PostMapping("/page")
    CommonResult<PageResult<OrderReceiveDto>> queryPage(@RequestBody ReceiveOrderPageQueryReq receiveOrderPageQueryReq) {
        OrderReceiveQueryDto receiveQueryDto = BeanUtil.copyProperties(receiveOrderPageQueryReq, OrderReceiveQueryDto.class);
        processQueryDate(receiveOrderPageQueryReq, receiveQueryDto);
        PageResult<OrderReceiveDto> page = receiveOrderAppService.queryPage(receiveQueryDto);
        List<OrderReceiveDto> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        return buildPageResult(page, list);
    }

    @Operation(summary = "收款单订单查询")
    @PostMapping("/payInfo")
    CommonResult<PageResult<PayOrderDetailResp>> orderPage(@RequestBody OrderReceivePageQueryReq orderReceivePageQueryReq) {
        PayOrderQueryDto payOrderQueryDto = net.dreamlu.mica.core.utils.BeanUtil.copy(orderReceivePageQueryReq, PayOrderQueryDto.class);
        processQueryDate(orderReceivePageQueryReq, payOrderQueryDto);
        // processQuerySn(payOrderPageQueryReq, payOrderQueryDto);
        PageResult<PayOrderDetailResp> page = receiveOrderAppService.queryPayPage(payOrderQueryDto);

        if (orderReceivePageQueryReq.getExport() == 1) {
            String fileName = orderReceivePageQueryReq.getExportName();
            if (!StringUtils.hasText(fileName)) fileName = "订单导出_" + DateUtils.getDateString(DatePattern.PURE_DATETIME_PATTERN);
            downloadExcelService.createExcel(fileName, payOrderQueryDto, PayOrderFileExport.class);
            return CommonResult.success();
        } else {
            return CommonResult.success(page);

        }
    }

    @Operation(summary = "保存收款单")
    @PostMapping("/save")
    CommonResult<OrderReceiveDO> save(@Validated @RequestBody ReceiveOrderSetReq ReceiveOrderSetReq) {
        return receiveOrderAppService.saveReceiveOrder(ReceiveOrderSetReq);
    }

    @Operation(summary = "更新收款单")
    @PostMapping("update")
    CommonResult<Void> update(@Validated @RequestBody ReceiveOrderUpdateReq receiveOrderUpdateReq) {
        return receiveOrderAppService.updateReceiveOrder(receiveOrderUpdateReq);
    }

    @Operation(summary = "收款单链接")
    @GetMapping("/link")
    CommonResult<String> link(@RequestParam(value = "code") String code) {
        return receiveOrderAppService.link(code);
    }

    @Operation(summary = "收款单详情")
    @GetMapping("/detail")
    CommonResult<OrderReceiveDto> detail(@RequestParam(value = "code") String code) {
        return receiveOrderAppService.detail(code);
    }

    @Operation(summary = "收款单更新状态")
    @PostMapping("/status")
    CommonResult<Void> receiveOrderAppService(@Validated @RequestBody ReceiveOrderUpdateReq receiveOrderUpdateReq) {
        return receiveOrderAppService.updateStatus(receiveOrderUpdateReq);
    }


    @Operation(summary = "提交收款单")
    @PostMapping("/submit")
    CommonResult<PayOrderSubmitApiResp> submitReceiveOrder(@Validated @RequestBody ReceiveOrderParamReq receiveOrderParamReq) {
        return receiveOrderAppService.submitReceiveOrder(receiveOrderParamReq);
    }

}
